﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormRkMesList
    Public usr As String
    Dim objCPCKIN As New clsCPCKIN
    Dim httpHelper As New HttpHelper
    Dim txt As TextBox
    Private Sub FormRkMesList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startDate As String = Date.Now.AddDays(-1).ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        setDgvData()
    End Sub

    Private Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            Dim dt As DataTable = objCPCKIN.getWarehouseInList(txtStartDate.Text.Trim() + " 00:00:00.000", txtEndDate.Text.Trim() + " 23:59:59.999")
            dt.Columns.Add("area_name")
            dt.Columns.Add("mes_sync_status_name")
            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    If row("area") = "ZAGZ01" Then
                        row("area_name") = "隔震成品库"
                    End If
                    If row("area") = "ZAGZ01A" Then
                        row("area_name") = "隔震成品库_A区"
                    End If
                    If row("area") = "ZAGZ01B" Then
                        row("area_name") = "隔震成品库_B区"
                    End If
                    If row("area") = "ZAGZ01C" Then
                        row("area_name") = "隔震成品库_C区"
                    End If
                    If row("area") = "ZAGZ01D" Then
                        row("area_name") = "隔震成品库_D区"
                    End If
                    If row("area") = "ZAGZ01E" Then
                        row("area_name") = "隔震成品库_E区"
                    End If
                    If row("mes_sync_status") = "0" Then
                        row("mes_sync_status_name") = "未同步"
                    End If
                    If row("mes_sync_status") = "01" Then
                        row("mes_sync_status_name") = "已同步,同步失败"
                    End If
                    If row("mes_sync_status") = "1" Then
                        row("mes_sync_status_name") = "已同步,同步成功"
                    End If
                Next
            End If
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dt
        Else
            MsgBox("请输入开始时间和结束时间")
        End If
    End Sub

    Private Sub btnRkMes_Click(sender As Object, e As EventArgs) Handles btnRkMes.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim oper_num As String = dgv.SelectedRows(0).Cells("Col_warehouse_in_count").Value.ToString()
            Dim warehousecode As String = dgv.SelectedRows(0).Cells("Col_area").Value.ToString()
            Dim productorname As String = dgv.SelectedRows(0).Cells("Col_prod_spec_id").Value.ToString()
            Dim MesProjectCode As String = dgv.SelectedRows(0).Cells("Col_mes_project_code").Value.ToString()
            Dim sys_id As String = dgv.SelectedRows(0).Cells("Col_sys_id").Value.ToString()
            mesRk(oper_num, warehousecode, productorname, MesProjectCode, sys_id)
            setDgvData()
            MsgBox("操作成功，请到同步MES入库历史记录里面，查看执行结果！", vbYes, "提示")
        Else
            MsgBox("请选中同步MES入库信息！", vbYes, "提示")
        End If
    End Sub

    Private Sub mesRk(oper_num As String, warehousecode As String, productorname As String, MesProjectCode As String, sys_id As String)
        Dim params As String = "{"
        params &= """project_code"":""" & MesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "装配" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103105""" '部门id
        params &= "}"
        Dim rtn As String = httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("操作成功") Then
            objCPCKIN.update("1", msg, usr, sys_id)
        Else
            objCPCKIN.update("01", msg, usr, sys_id)
        End If
    End Sub

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub
End Class